package com.sgcc.pda.frme.protocol.ws.sendbean;

import androidx.annotation.NonNull;

import android.text.TextUtils;
import android.util.Log;

import com.sgcc.pda.frme.protocol.ws.DataConvert;

/**
 * @author guorongxin
 * @date 2020/3/25
 * @fileName hw
 * @Description
 * @special 如修改此类请在下方添加备注并告知创建者，谢谢配合！
 * @VERSION 1.0
 */
public class EpcWriteDataParams2009 {

    /**
     * 安全标识
     */
    private String safeType;

    /**
     * 数据区
     */
    private String dataSpace;

    /**
     * 偏移
     */
    private String offset;

    /**
     * 写入数据长度
     */
    private String length;

    /**
     * 标签访问密码
     */
    private String password;

    /**
     * 数据区
     */
    private String data;

    public String getSafeType() {
        return safeType;
    }

    public void setSafeType(String safeType) {
        this.safeType = safeType;
    }

    public String getDataSpace() {
        return dataSpace;
    }

    public void setDataSpace(String dataSpace) {
        this.dataSpace = dataSpace;
    }

    public String getOffset() {
        return offset;
    }

    public void setOffset(String offset) {
        this.offset = offset;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        if (null == safeType) {
            return null;
        }
        safeType = DataConvert.toHexString(Integer.valueOf(safeType), 1);
        sb.append(safeType);
        if (null == dataSpace) {
            return null;
        }
        dataSpace = DataConvert.toHexString(Integer.valueOf(dataSpace), 1);
        sb.append(dataSpace);
        if (null == offset) {
            return null;
        }
        offset = DataConvert.toHexString(Integer.valueOf(offset), 2);
        sb.append(DataConvert.strReverse(offset, 0, offset.length()));

        if (null == password) {
            return null;
        }
        //密码无需再进行转换
//        password = DataConvert.toHexString(Integer.valueOf(password), 4);
        password = DataConvert.strReverse(password, 0, password.length());
        Log.e("EpcWriteDataParams2009", "password:" + password);
        sb.append(password);

        if (null == length) {
            return null;
        }
        sb.append(DataConvert.toHexString(Integer.valueOf(length), 1));
        if (null == data) {
            return null;
        }
        if (data.length() != Integer.valueOf(length) * 4) {
            data = lengthfilling(data, Integer.valueOf(length) * 4);
        }
        sb.append(DataConvert.strReverse(data, 0, data.length()));
        return sb.toString();
    }

    public String lengthfilling(String meterAddress, int length) {
        if (!TextUtils.isEmpty(meterAddress) && meterAddress.length() < length) {
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < length - meterAddress.length(); i++) {
                stringBuffer.append("0");
            }
            stringBuffer.append(meterAddress);
            meterAddress = stringBuffer.toString();
            return meterAddress;
        }
        return "";
    }
}
